"use strict";
/* Controllers */
app.controller("xinghuoController", ['$scope', '$location', '$rootScope', '$routeParams', 'xinghuoService', function ($scope, $location, $rootScope, $routeParams, xinghuoService) {

    var vm = $scope.vm = {};
    vm.result = {};
    vm.p = {};
    vm.p.id = $routeParams.id; //
    var route_nodeId=$routeParams.nodeId;
    vm.p.url = "";
    vm.p.dashboardType = 'report';

    var errorFn = function (result) {
        alert(result);
    };
    //$scope.changeUri = function(id, href) {
    //    var vm = $scope.vm;
    //    $location.search('id',id);
    //};

    $scope.loadUrl = function (id) {
        var vm = $scope.vm;
        // 请求后台
        vm.result.ms = "...";
        var param = {dashboardType: vm.p.dashboardType};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            // 匹配节点
            var findText = function(arr, text) {
                var idx = -1;
                for(var i=0;i<arr.length;++i) {
                    if(arr[i].text==text) {
                        idx = i;
                        break;
                    }
                }
                return idx;
            };
            var addNode = function(tree, biz, group, node) {
                var bizNode;
                var bizIdx = findText(tree, biz);
                if(bizIdx==-1) {
                    bizNode = {text: biz, nodes:[]};
                    tree.push(bizNode);
                }else {
                    bizNode = tree[bizIdx];
                }

                var groupNode;
                var groupIdx = findText(bizNode.nodes, group);
                if(groupIdx==-1) {
                    groupNode = {text: group, nodes:[]};
                    bizNode.nodes.push(groupNode);
                }else {
                    groupNode = bizNode.nodes[groupIdx];
                }
                groupNode.nodes.push(node);
            };
            var encodeTreeData = function(data) {
                var tree =[];
                for(var i=0; i<data.length; ++i) {
                    var dashboard = {id: data[i].id, text: data[i].dashboardName, frame_href: data[i].dashboardHref};
                    addNode(tree,data[i].bizName, data[i].groupName, dashboard);
                }
                return tree;
            };
            var treeData = encodeTreeData(data);

            $('#tree').treeview({
                data: treeData,
                levels: 5,
//                enableLinks:true,
                onNodeSelected: function (event, data) {
                    if(data.frame_href==null) return;
                    $("#preview").attr("href",data.frame_href)
                    $('#iframe_page').attr("src",data.frame_href);
                    history.pushState({id:data.id},null,"#/star/report/xinghuo/0/"+data.id);
                }
            });
            if(typeof(route_nodeId)!="undefined"&&route_nodeId!=null){
                var nodeid=$("[data-node-id="+route_nodeId+"]").attr("data-nodeid")
                $('#tree').treeview('selectNode', [ Number(nodeid), { silent: false } ]);
            }
        };
        xinghuoService.loadDashboard(param, procFn, errorFn);
    };

    // 重新加载
    $scope.loadUrl(vm.p.id);

    //$scope.$on("$locationChangeSuccess", function () {
    //    //监听url变化，在变化后作想要的处理
    //    alert("success.");
    //    return false;
    //});
}]);


/* Services */
app.service('xinghuoService', ['ajaxService', function (ajaxService) {
    this.loadDashboard = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dashboard/loadDashboard.do", successFunction, errorFunction);
    };
}]);


